Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<ToolboxData("<{0}:SampleDatabound runat=server></{0}:SampleDatabound>")> _
Public Class SampleDatabound
    Inherits WebControls.DataBoundControl

    Public Property DataTextField() As String
        Get
            Dim strDTF As String
            strDTF = ViewState("DTF").ToString
            If strDTF Is Nothing Then
                Return String.Empty
            Else
                Return strDTF
            End If
        End Get
        Set(ByVal value As String)
            If Me.Initialized = True Then
                Me.OnDataPropertyChanged()
            End If
            ViewState("DTF") = value
        End Set
    End Property

    Protected Overrides Sub PerformSelect()
        Dim dsa As System.Web.UI.DataSourceSelectArguments
        Dim dsv As System.Web.UI.DataSourceView

        If Me.IsBoundUsingDataSourceID = False Then
            Me.OnDataBinding(EventArgs.Empty)
        End If
        dsa = New System.Web.UI.DataSourceSelectArguments
        dsa.MaximumRows = 10

        dsv = Me.GetData
        If dsv.CanRetrieveTotalRowCount = True Then
            dsa.RetrieveTotalRowCount = True
        End If
        dsv.Select(dsa, AddressOf Me.HandleRetrievedData)
        RequiresDataBinding = False
        MarkAsDataBound()
        OnDataBound(EventArgs.Empty)
    End Sub

    Private Sub HandleRetrievedData(ByVal data As IEnumerable)

        PerformDataBinding(data)

    End Sub

    Protected Overrides Sub PerformDataBinding( _
                       ByVal data As System.Collections.IEnumerable)
        Dim objData As Object
        Dim tb As System.Web.UI.WebControls.TextBox
        Dim ctr As Integer
        Dim strFieldName As String

        MyBase.PerformDataBinding(data)

        If data IsNot Nothing Then
            If DataTextField > "" Then
                strFieldName = DataTextField
                ctr = 0
                For Each objData In data
                    tb = New System.Web.UI.WebControls.TextBox
                    ctr = ctr + 1
                    tb.ID = "tb" & ctr
                    tb.Text = System.Web.UI.DataBinder.GetPropertyValue( _
                                             objData, strFieldName, Nothing)
                    Me.Controls.Add(tb)
                Next
            End If
        End If
    End Sub

    Public Sub InsertData()
        Dim InsertData As New StateBag
        Dim dsv As System.Web.UI.DataSourceView

        dsv = Me.GetData
        If dsv.CanInsert = True Then
            InsertData.Add("CustId", CType(Me.FindControl("tb1"), TextBox).Text)
            InsertData.Add("CustName", CType(Me.FindControl("tb2"), TextBox).Text)
            dsv.Insert(InsertData, AddressOf Me.HandleInsert)
        End If

    End Sub

    Function HandleInsert(ByVal affectedRecords As Integer, _
                                       ByVal ex As Exception) As Boolean
        If ex IsNot Nothing Then
            Throw ex
        End If

    End Function



End Class
